package com.dmgis.qxfw.dataservice.businessservice.yydl.service.Impl;


import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.xt.sys_config;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.yydl.service.UtilServiceI;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class UtilServiceImpl implements UtilServiceI {

	ProcessRequestController pc = new ProcessRequestController();
	
	@Override
	public String getValueByName(String value) {
		String result = "";
		List<sys_config> resultList = new ArrayList<sys_config>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("paramtype");
		columns.add("paramname");
		columns.add("paramvalue");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (value!=null) {
			strWhere.append(String.format(" and paramname='%s'",value));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "sys_config");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			resultList = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<sys_config>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if(resultList!=null && resultList.size()!=0){
			result = resultList.get(0).getParamvalue();
		}
		
		return result;
	}

}
